﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.Data.Browsing.Design;

namespace 富诚_库存账龄分析看板
{
    public partial class Setting : Form
    {
        public Setting()
        {
            InitializeComponent();
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        private static Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        //更新方法
        public static void UpdateAppSettings(string key, string value)
        {
            if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
            {
                //如果当前节点存在，则更新当前节点
                config.AppSettings.Settings[key].Value = value;
                config.Save(ConfigurationSaveMode.Modified);
            }
            else
            {
                Console.WriteLine("当前节点不存在");
            }
        }

        //加载
        private void Setting_Load(object sender, EventArgs e)
        {
            panelTable.Top = panelBasic.Top + panelBasic.Height + 1;
            panelLine.Top = panelTable.Top + panelTable.Height + 1;
            panelBar.Top = panelLine.Top + panelLine.Height + 1;
            Save.Left = (this.Width - Save.Width) / 2;

            //Organization
            OrganizationName.Text = ConfigurationManager.AppSettings["Organization"]; 
            //basic setting
            TimeRefresh.Text = ConfigurationManager.AppSettings["TimeRefresh"];
            //table
            TableDay1.Text = ConfigurationManager.AppSettings["TableDay1"];
            TableDay2.Text = ConfigurationManager.AppSettings["TableDay2"];
            TableDay3.Text = ConfigurationManager.AppSettings["TableDay3"];
            TableDay4.Text = ConfigurationManager.AppSettings["TableDay4"];
            TableMonth.Text = ConfigurationManager.AppSettings["TableMonth"];
            //line
            LineYear.Text = ConfigurationManager.AppSettings["LineYear"];
            LineMonth.Text = ConfigurationManager.AppSettings["LineMonth"];
            //bar
            BarYear.Text = ConfigurationManager.AppSettings["BarYear"];
            BarMonth.Text = ConfigurationManager.AppSettings["BarMonth"];
            BarDay.Text = ConfigurationManager.AppSettings["BarDay"];
        }

        //保存
        private void Save_Click(object sender, EventArgs e)
        {
            //Organization
            UpdateAppSettings("Organization", OrganizationName.Text);
            //basic setting
            UpdateAppSettings("TimeRefresh", TimeRefresh.Text);
            //table
            UpdateAppSettings("TableDay1", TableDay1.Text);
            UpdateAppSettings("TableDay2", TableDay2.Text);
            UpdateAppSettings("TableDay3", TableDay3.Text);
            UpdateAppSettings("TableDay4", TableDay4.Text);
            UpdateAppSettings("TableMonth", TableMonth.Text);
            //line
            UpdateAppSettings("LineYear", LineYear.Text);
            UpdateAppSettings("LineMonth", LineMonth.Text);
            //bar
            UpdateAppSettings("BarYear", BarYear.Text);
            UpdateAppSettings("BarMonth", BarMonth.Text);
            UpdateAppSettings("BarDay", BarDay.Text);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #region panel样式
        private void panelBasic_Paint(object sender, PaintEventArgs e)
        {
            //使用红色虚线绘制边框
            Pen pen1 = new Pen(Color.Red, 1);
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pen1.DashPattern = new float[] { 4f, 2f };
            e.Graphics.DrawRectangle(pen1, 0, 0, this.panelBasic.Width - 1, this.panelBasic.Height - 1);
        }
        private void panelTable_Paint(object sender, PaintEventArgs e)
        {
            //使用红色虚线绘制边框
            Pen pen1 = new Pen(Color.White, 1);
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pen1.DashPattern = new float[] { 4f, 2f };
            e.Graphics.DrawRectangle(pen1, 0, 0, this.panelTable.Width - 1, this.panelTable.Height - 1);
        }

        private void panelLine_Paint(object sender, PaintEventArgs e)
        {
            //使用红色虚线绘制边框
            Pen pen1 = new Pen(Color.White, 1);
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pen1.DashPattern = new float[] { 4f, 2f };
            e.Graphics.DrawRectangle(pen1, 0, 0, this.panelLine.Width - 1, this.panelLine.Height - 1);
        }

        private void panelBar_Paint(object sender, PaintEventArgs e)
        {
            //使用红色虚线绘制边框
            Pen pen1 = new Pen(Color.White, 1);
            pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pen1.DashPattern = new float[] { 4f, 2f };
            e.Graphics.DrawRectangle(pen1, 0, 0, this.panelBar.Width - 1, this.panelBar.Height - 1);
        }
        #endregion
    }
}
